package com.zking.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zking.dao.domain.Register;
import com.zking.dao.domain.RegisterMonth;
import com.zking.service.RegisterService;
import com.zking.utils.DateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Tanglei
 * @date 2020/8/21 - 15:39
 */
@Controller
@RequestMapping(value = "/register",name = "挂号控制器")
public class RegisterController {

    @Autowired
    RegisterService registerService;

    @RequiresPermissions("register:registerDetail")
    @RequestMapping(value = "/registerDetail",name = "查看挂号详情")
    public String registerDetail(Model model){
        List<Register> registers1 = registerService.queryAllRegister();
        for (Register register : registers1) {
            //预约时间
            String regTime = register.getCreateTime();
            //截取预约时间
            String regTimef = regTime.substring(0, 10) + regTime.substring(17,22);
            //系统当前时间
            String currentTime = DateUtil.getCurrentTime();
            int compare = regTimef.compareTo(currentTime);
            if(compare < 0){//说明预约时间小于当前时间，更新数据
                registerService.updateRegState(register.getId());
            }
        }
        List<Register> registers = registerService.queryAllRegister();
        model.addAttribute("registers",registers);
        return "backend_page/appointments";
    }

    @RequestMapping(value = "/registerNumsMonthly",name = "挂号量按月统计")
    @ResponseBody
    public String registerNumsMonthly(){
        List<RegisterMonth> registerMonths = registerService.registerNumsMonthly();
        JSONArray dataStr= JSONArray.parseArray(JSON.toJSONString(registerMonths));
        return dataStr.toString();
    }
}
